package org.example.oop.thread;

import lombok.SneakyThrows;

/**
 * org.example.oop.thread
 *
 * @author moLei
 * @description 一共有1000张电影票，可以在两个窗口领取，假设每次领取的时间为3000毫秒
 *      要求：请用多线程模拟买票过程，并打印剩余电影票的数量
 * @date 2025/6/25
 */
public class MoveTicketExercises {

    private static int ticketCount = 1000;

    public static void main(String[] args) {
        // 窗口1
        new Thread(() -> {
            while (ticketCount > 0) {
                saleTicket();
                // 领取时间为3000毫秒
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "窗口1").start();

        // 窗口2
        new Thread(() -> {
            while (ticketCount > 0) {
                saleTicket();
                // 领取时间为3000毫秒
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "窗口2").start();
    }

    /**
     * 买票程序
     */
    @SneakyThrows
    public synchronized static void saleTicket() {
            System.out.println(Thread.currentThread().getName() + "正在卖票，剩余" + --ticketCount + "张票");
    }
}
